package handlers

import (
	"admin-ease/cache"
	"admin-ease/models"
	"admin-ease/pkg"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	var user models.User
	redis_key := "login_" + username
	errorData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errorData >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误3次，禁止登录",
		})
		return
	}
	err := models.DB.Where("username=?", username).Debug().First(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if user.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if pkg.Md5(password) != user.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errorData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次，封禁2小时",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": user,
	})
	return
}
func CreateRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	nid, _ := strconv.Atoi(c.Query("nid"))
	if nid == 0 || rid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "关联成功",
	})
	return
}
func UserRole(c *gin.Context) {
	uid, _ := strconv.Atoi(c.Query("uid"))
	/*
		   SELECT
		*
		FROM
		roles
		LEFT JOIN user_roles ON roles.id=user_roles.rid
	*/
	var role models.Role
	models.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id=user_roles.rid").
		Where("user_roles.uid=?", uid).
		Debug().Find(&role)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": role,
	})
	return
}
func UserNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.Query("rid"))
	/*
			SELECT
		*
		FROM
		nodes
		LEFT JOIN role_nodes ON nodes.id=role_nodes.nid
	*/
	var node models.Node
	models.DB.Table("nodes").
		Joins("LEFT JOIN role_nodes ON nodes.id=role_nodes.nid").
		Where("role_nodes.rid=?", rid).
		Debug().Find(&node)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": node,
	})
	return
}
func CreateUser(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.User{
		Username: username,
		Password: password,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})
	return
}

//func List(c *gin.Context) {
//	uid, _ := strconv.Atoi(c.Query("uid"))
//	/*
//		SELECT
//		*
//		FROM
//		  orders
//		  LEFT JOIN users ON users.id=orders.uid
//	*/
//	var userOrder models.UserOrder
//	models.DB.Table("orders").
//		Joins("LEFT JOIN users ON users.id=orders.uid").
//		Where("orders.uid=?", uid).
//		Debug().Find(&userOrder)
//	c.JSON(200, gin.H{
//		"code": 200,
//		"msg":  "ok",
//		"data": userOrder,
//	})
//	return
//}
